<?php
/**
 * 后台今日食谱功能模块
 */
#定义命名空间
namespace Admin\Controller;
class FoodController extends ComController {
    
    /**
     *  每周食谱列表
     */
    public function index(){
        $sc_id = session('sc_id');//学校id
        $food = M('food');//实例化食谱主表
        $prefix = C('DB_PREFIX');//读取数据表的前缀
        $where["{$prefix}food.sc_id"] = $sc_id;
        // $where["{$prefix}food.is_del"] = 1;
        $p = I('get.p') ? I('get.p') : 1;//接收页数
        $pagesize = 15;//每页显示条数
        $offset = ($p-1)*$pagesize; //偏移量
        $order =  " {$prefix}food.add_time";//排序
        $count = $food->where(' sc_id = '.$sc_id)->count();//总条数
        $food_info = $food->field("{$prefix}food.*,{$prefix}member.name as teacher_name")->join("inner join {$prefix}member on {$prefix}member.uid = {$prefix}food.doctor")->where($where)->order($order)->limit($offset.','.$pagesize)->select();
        $data = array();
        foreach ($food_info as $k => $v) {  
            $data[$k]['id'] = $v['id'];
            $data[$k]['teacher_name'] = $v['teacher_name'];
            $data[$k]['title'] = $v['title'];
            $data[$k]['add_time'] = $v['add_time'];
            $data[$k]['is_del'] = $v['is_del'];
            $se = $this->getSe($v['id']);
            $data[$k]['start'] = $se['start'];
            $data[$k]['end'] = $se['end'];
        }
        // echo json_encode($data);die;
        $page = new \Think\Page($count,$pagesize);
        $page = $page->show();
        $this->assign('page',$page);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 通过食谱ID获取 本周食谱开始时间-结束时间
     */
    public function getSe($id = 0){
        $food_time = M('food_time');
        $prefix = C('DB_PREFIX');//读取数据表的前缀
        $data = $food_time->field("Min({$prefix}food_time.time) as start ,Max({$prefix}food_time.time) as end")->where(' food_id ='.$id)->find();
        if($data) return $data;
        else return array('start'=>0,'end'=>0);
    }

    /**
     * 添加一周食谱菜单
     */
    public function append(){
        $sc_id = session('sc_id');//学校id
        if(IS_POST){
            //表单提交
            $param = I('post.');//接收参数
            $title = $param['title'] ? trim($param['title']) : '';
            $doctor = $param['doctor'] ? $param['doctor'] : 0;
            $bf = $param['bf']; //早餐
            $bf_time = $param['bf_time']; //早餐时间
            $bf_ext = $param['bf_ext']; //中午加餐
            $bf_ext_time = $param['bf_ext_time']; //中午加餐时间
            $lunch = $param['lunch']; //午餐
            $l_time = $param['l_time']; //午餐时间
            $l_ext = $param['l_ext']; //下午加餐
            $l_ext_time = $param['l_ext_time']; //下午加餐时间
            $dinner = $param['dinner']; //晚餐
            $d_time = $param['d_time']; //晚餐时间
            $time = $param['time'];//日期
            //日期 格式化
            $newTime = array();
            foreach ($time as $key => $value) {
                $strTime = explode('-', $value);
                $newTime[] = strtotime($strTime[0]);
            }  

            $food = M('food');//食谱主表
            $food_time = M('food_time');//食谱辅表
            $prefix = C('DB_PREFIX');//读取数据表的前缀

            if(!$doctor){
                $this->error('请选择保健老师!');die;
            }
            if(empty($title)){
                $this->error('请填写标题!');die;
            }
            foreach ($bf as $k => $v) {
                if(empty($v) && empty($lunch[$k]) && empty($dinner[$k])){
                    $this->error('早餐、午餐、晚餐,必须选择一种！');
                    break;
                }
            }
            $isExt = $food_time
                    ->join("inner join {$prefix}food on {$prefix}food_time.food_id = {$prefix}food.id")
                    ->where(array("{$prefix}food.sc_id"=>$sc_id,"{$prefix}food_time.time"=>array("in",$newTime)))->find(); //验证本周是否已经发布了食谱信息
            if($isExt){
                $this->error('本周食谱已经发布，不能重复发布!');
            }
            //组装数据 
            $food_data['title'] = $title;
            $food_data['doctor'] = $doctor;
            $food_data['sc_id'] = $sc_id;
            $food_data['add_time'] = time();
            $food_id = $food->add($food_data);
            if($food_id){
                //组装数据
                $ft_data = array();
                foreach ($bf as $k => $v) {
                    $ft_data[$k]['bf'] = $v;
                    $ft_data[$k]['bf_time'] = $bf_time[$k];
                    $ft_data[$k]['bf_ext'] = $bf_ext[$k];
                    $ft_data[$k]['bf_ext_time'] = $bf_ext_time[$k];
                    $ft_data[$k]['lunch'] = $lunch[$k];
                    $ft_data[$k]['l_time'] = $l_time[$k];
                    $ft_data[$k]['l_ext'] = $l_ext[$k];
                    $ft_data[$k]['l_ext_time'] = $l_ext_time[$k];
                    $ft_data[$k]['dinner'] = $dinner[$k];
                    $ft_data[$k]['d_time'] = $d_time[$k];
                    $ft_data[$k]['time'] = $newTime[$k];
                    
                    $ft_data[$k]['food_id'] = $food_id;
                    $ft_data[$k]['add_time'] = time();
                    $ft_data[$k]['sc_id'] = $sc_id;
                }
                $food_time_res = $food_time->addAll($ft_data);
                if($food_time_res){
                    $this->error('添加成功!',U('index'));
                }else{
                   //添加失败
                    //删除课程主表对应信息
                    $food->where(array('id'=>$food_id))->delete();
                    // echo $food_time->getError();die;
                    $this->error('添加失败!'); 
                }
            }else{
                $this->error('发布失败!');
            }
        }else{
            //展示页面    
            $teacher = $this->getTeacher();
            $this->assign('teacher',$teacher);
            $this->display();
        }
    }

    /**
     * 根据id修改 某一周的菜单
     * @param   $id 食谱主表的主键id
     */
    public function edit(){
        $id = I('get.id') ? I('get.id') : 0;
        $food = M('food');//食谱主表
        $food_time = M('food_time');//食谱外链表
        if(IS_POST){
            //表单提交
            $param = I('post.');//接收参数
            $id = $param['id']; //要修改食谱的主表id
            $title = $param['title'] ? trim($param['title']) : '';
            $doctor = $param['doctor'] ? trim($param['doctor']) : 0;
            $bf = $param['bf']; //早餐
            $bf_time = $param['bf_time']; //早餐时间
            $bf_ext = $param['bf_ext']; //中午加餐
            $bf_ext_time = $param['bf_ext_time']; //中午加餐时间
            $lunch = $param['lunch']; //午餐
            $l_time = $param['l_time']; //午餐时间
            $l_ext = $param['l_ext']; //下午加餐
            $l_ext_time = $param['l_ext_time']; //下午加餐时间
            $dinner = $param['dinner']; //晚餐
            $d_time = $param['d_time']; //晚餐时间
            //验证
            if(!$doctor){
                $this->error('请选择保健老师!');die;
            }
            if(empty($title)){
                $this->error('请填写标题!');die;
            }
            foreach ($bf as $k => $v) {
                if(empty($v) && empty($lunch[$k]) && empty($dinner[$k])){
                    $this->error('早餐、午餐、晚餐,必须选择一种！');
                    break;
                }
            }
            //组装数据 修改
            $food_data['title'] = $title;
            $food_data['doctor'] = $doctor;
            $food_data['update_time'] = time();
            $food_where['id'] = $id;
            $food_res = $food->where($food_where)->save($food_data);
            //获取食谱外链表的id
            $ft_data = $food_time->where('food_id ='.$id)->select();
            $ft_ids = array();
            foreach ($ft_data as $key => $value) {
                $ft_ids[$key] = $value['id'];
            }
            //组装食谱表外链表数据
            $ft_data = array();
            foreach ($bf as $k => $v) {
                $ft_data[$k]['bf'] = $v;
                    $ft_data[$k]['bf_time'] = $bf_time[$k];
                    $ft_data[$k]['bf_ext'] = $bf_ext[$k];
                    $ft_data[$k]['bf_ext_time'] = $bf_ext_time[$k];
                    $ft_data[$k]['lunch'] = $lunch[$k];
                    $ft_data[$k]['l_time'] = $l_time[$k];
                    $ft_data[$k]['l_ext'] = $l_ext[$k];
                    $ft_data[$k]['l_ext_time'] = $l_ext_time[$k];
                    $ft_data[$k]['dinner'] = $dinner[$k];
                    $ft_data[$k]['d_time'] = $d_time[$k];
                    // $ft_data[$k]['time'] = $newTime[$k];
                    
                    $ft_data[$k]['update_time'] = time();
            }
            //循环修改
            foreach ($ft_ids as $k => $v) {
                $ft_res = $food_time->where('id = '.$v)->save($ft_data[$k]);
                if(!$ft_res){
                    $this->error('修改失败!');
                    break;
                }
            }
            $this->success('修改成功!');
        }else{
            
            if($id){
                $food_data = $food->where(' id = '.$id)->find();
                if($food_data){
                    $food_id = $food_data['id'];
                    $ft_data = $food_time->where('food_id = '.$food_id)->select();
                    $food_data['time'] = $ft_data;
                    $teacher = $this->getTeacher();
                    $this->assign('data',$food_data);
                    $this->assign('teacher',$teacher);
                    $this->display();
                }else{
                    $this->error('没有数据!');
                }
            }else{
                $this->error('参数错误!',U('index'));
            }
        }
    }

    /**
     * 根据id删除 某一周的菜单
     * @param   $id 食谱主表的主键id
     */
    public function del(){
        $id = I('get.id') ? I('get.id') : 0;//获取id
        if($id){
            $food = M('food');
            $food_time = M('food_time');
            $food_data = $food->where('id = '.$id)->find();//实例化课程主表
            if($food_data){
                $food_res = $food->where(' id = '.$id)->delete();
                $food_time_res = $food_time->where(' food_id = '.$id)->delete();
                if($food_res && $food_time_res){
                    $this->error('删除成功!',U('index'));
                }else{
                    $this->error('删除失败!',U('index'));
                }
            }else{
                $this->error('数据不存在!',U('index'));
            }
        }else{
            $this->error('缺少参数!',U('index'));
        }
    }

    /**
     * ajax 改变显示状态
     * @param   $id 食谱主表的主键id
     */
    public function is_show(){
        $param = I('post.is_show') ? I('post.is_show') : 0;
        $param = explode('-', $param);
        $is_show = $param['0'];
        $id = $param['1'];
        if($id){
            $food = M('food');
            $food_data = $food->where('id = '.$id)->find();//实例化课程主表
            if($food_data){
                $food_res = $food->where(' id = '.$id)->save(array('is_del'=>$is_show));
                if($food_res !== false){
                    echo json_encode(array('res'=>1,'msg'=>'ok'));
                }else{
                    echo json_encode(array('res'=>0,'msg'=>'error'));
                }
            }else{
                echo json_encode(array('res'=>0,'msg'=>'noData'));
            }
        }else{
            echo json_encode(array('res'=>0,'msg'=>'noData'));
        }
    }

    /**
     * 获取保健老师信息
     * @return   array 老师信息的二维数组
     */
    public function getTeacher(){
        $sc_id = session('sc_id');//当前学校id
        $member = M('member');//实例化教师|学校表
        $where['pid'] = $sc_id;
        $where['level'] = 2;//老师
        $where['teacher_type'] = 4;//保健老师
        $teacher = $member->field('uid,name')->where($where)->select();
        return $teacher;
    }

    /**
     * 根据食谱id 查看食谱详情
     * @param int $id 
     */
    public function detail(){
        $id = I('get.id') ? I('get.id') : 0;//获取id
        $sc_id = session('sc_id');//学校id
        if($id){
                $food = M('food');//食谱主表
                $food_time = M('food_time');//食谱外链表
                $food_data = $food->where(' id = '.$id)->find();
                if($food_data){
                    $food_id = $food_data['id'];
                    $ft_data = $food_time->where('food_id = '.$food_id)->select();
                    $food_data['time'] = $ft_data;
                    $teacher = $this->getTeacher();
                    $this->assign('data',$food_data);
                    $this->assign('teacher',$teacher);
                    $this->display();
                }else{
                    $this->error('没有数据!');
                }
            }else{
                $this->error('参数错误!',U('index'));
            }
    }
}